package com.bawei.mvvmzonghe;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.bawei.mvvmzonghe.adapter.CateGoryAdapter;
import com.bawei.mvvmzonghe.adapter.CateGoryTopAdapter;
import com.bawei.mvvmzonghe.adapter.GoodsAdapter;
import com.bawei.mvvmzonghe.databinding.ActivityMainBinding;
import com.bawei.mvvmzonghe.entity.BaseResponseEntity;
import com.bawei.mvvmzonghe.entity.CateGoryEntity;
import com.bawei.mvvmzonghe.entity.GoodsEntity;
import com.bawei.mvvmzonghe.viewmodel.GoodsViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

/**
 *
 * 数据来源：网络/数据库
 * （1）网络请求成功将数据库存储起来
 *
 *
 */
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mActivityMainBinding;
    private GoodsViewModel mGoodsViewModel;
    private CateGoryAdapter mCateGoryAdapter;
    private CateGoryTopAdapter mCateGoryTopAdapter;
    private GoodsAdapter mGoodsAdapter;
    private List<CateGoryEntity> mCateGoryEntityList = new ArrayList<>();
    private List<CateGoryEntity> mCateGoryEntityTopList = new ArrayList<>();
    private List<GoodsEntity.ListBean> mList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        mGoodsViewModel = new ViewModelProvider(this).get(GoodsViewModel.class);
        initView();
        initListener();
        initData();
    }

    private void initListener() {
        //点击一级分类获得二级分类数据
        mCateGoryAdapter.setItemClickLister(new CateGoryAdapter.ItemClickLister() {
            @Override
            public void onItemClick(View view, int position) {
                int id = mCateGoryEntityList.get(position).getId();
                mGoodsViewModel.type(id).observe(MainActivity.this, new Observer<BaseResponseEntity<List<CateGoryEntity>>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<List<CateGoryEntity>> listBaseResponseEntity) {
                        List<CateGoryEntity> data = listBaseResponseEntity.getData();
                        mCateGoryEntityTopList.clear();
                        mCateGoryEntityTopList.addAll(data);
                        mCateGoryTopAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
        //点击二级列表获得商品数据
        mCateGoryTopAdapter.setItemClickLister(new CateGoryAdapter.ItemClickLister() {
            @Override
            public void onItemClick(View view, int position) {
                int id = mCateGoryEntityTopList.get(position).getId();

                mGoodsViewModel.goods(id,1,10).observe(MainActivity.this, new Observer<BaseResponseEntity<GoodsEntity>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<GoodsEntity> goodsEntityBaseResponseEntity) {
                        List<GoodsEntity.ListBean> list = goodsEntityBaseResponseEntity.getData().getList();
                        mList.clear();
                        mList.addAll(list);
                        mGoodsAdapter.notifyDataSetChanged();


                        //将数据存储到数据库中:主线程不能操作数据库:线程池中执行耗时操作
                        Executors.newCachedThreadPool().execute(new Runnable() {
                            @Override
                            public void run() {
                                mGoodsViewModel.insert(list);
                                Log.d("ytx", "run: 插入数据成功");
                            }
                        });

                    }
                });
            }
        });
    }

    private void initData() {
        //左边的一级分类
        mGoodsViewModel.type(0).observe(this, new Observer<BaseResponseEntity<List<CateGoryEntity>>>() {
            @Override
            public void onChanged(BaseResponseEntity<List<CateGoryEntity>> listBaseResponseEntity) {
                List<CateGoryEntity> data = listBaseResponseEntity.getData();
                mCateGoryEntityList.addAll(data);
                mCateGoryAdapter.notifyDataSetChanged();



            }
        });

    }

    private void initView() {
        mCateGoryAdapter = new CateGoryAdapter(mCateGoryEntityList);
        mActivityMainBinding.rvCategoryLeft.setAdapter(mCateGoryAdapter);
        mActivityMainBinding.rvCategoryLeft.setLayoutManager(new LinearLayoutManager(this));


        mCateGoryTopAdapter = new CateGoryTopAdapter(mCateGoryEntityTopList);
        mActivityMainBinding.rvCategoryTop.setAdapter(mCateGoryTopAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mActivityMainBinding.rvCategoryTop.setLayoutManager(linearLayoutManager);



        mGoodsAdapter = new GoodsAdapter(mList);
        mActivityMainBinding.rvGoods.setAdapter(mGoodsAdapter);
        mActivityMainBinding.rvGoods.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

    }
}